package aole.mindfx.model;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NamedNodeMap;

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;
import javafx.scene.control.Button;
import javafx.scene.text.Font;
import aole.mindfx.FXNodeListener;

public class MindFXAttribute extends MindFXNode implements
		EventHandler< ActionEvent > {
	private String			scope;
	private String			type;
	private String			defValue;
	private int				pointers;
	private Button			node;
	private FXNodeListener	listener;
	private MindFXClass	parent;

	private static Font		f	= new Font( "Cambria", 16 );

	public String getCaption() {
		String s = getType();
		for( int i = 0; i < getPointers(); i++ )
			s += "*";
		s += " " + getName();
		return s;
	}

	public String getFullCaption() {
		String s = getType();
		for( int i = 0; i < getPointers(); i++ )
			s += "*";
		s += " " + parent.getName()+"::"+getName();
		return s;
	}

	public static String getStyle() {
		return "-fx-base:blue";
	}

	public static Font getFont() {
		return f;
	}

	public String getScope() {
		return scope;
	}

	public int getPointers() {
		return pointers;
	}

	public void setPointers( int pointers ) {
		this.pointers = pointers;
		node.setText( getCaption() );
	}

	public void setScope( String scope ) {
		this.scope = scope;
	}

	public String getDefault() {
		return defValue;
	}

	public void setDefault( String v ) {
		this.defValue = v;
	}

	public String getType() {
		return type;
	}

	public void setType( String type ) {
		this.type = type;
		node.setText( getCaption() );
	}

	public MindFXAttribute( String n, FXNodeListener l, MindFXClass c ) {
		super( n );
		scope = "public";
		type = "int";
		pointers = 0;
		listener = l;
		parent = c;

		node = new Button( getCaption() );
		node.setStyle( getStyle() );
		node.setFont( getFont() );
		node.setOnAction( this );
	}

	@Override
	public boolean setXMLAttribute( String att, String value ) {
		if( super.setXMLAttribute( att, value ) ) return true;

		switch( att ) {
		case "scope":
			setScope( value );
			return true;
		case "type":
			setType( value );
			return true;
		case "default":
			setDefault( value );
			return true;
		case "pointers":
			setPointers( Integer.parseInt( value ) );
			return true;
		}
		return false;
	}

	@Override
	public Node getFXNode() {
		return node;
	}

	@Override
	public void handle( ActionEvent event ) {
		listener.actionPerformed( this );
	}

	@Override
	public void saveXML( Document doc, Element e ) {
		e.setAttribute( "type", getType() );
		e.setAttribute( "scope", getScope() );
		e.setAttribute( "pointers", "" + pointers );
		if( defValue != null && !defValue.equals( "" ) )
			e.setAttribute( "default", defValue );
	}

	public MindFXClass getParent() {
		return parent;
	}

	@Override
	public void loadXML( org.w3c.dom.Node node ) {
		// get attributes
		NamedNodeMap atts = node.getAttributes();
		for( int i = 0; i < atts.getLength(); i++ ) {
			org.w3c.dom.Node att = atts.item( i );
			if( att.getNodeType() == org.w3c.dom.Node.ATTRIBUTE_NODE ) {
				if( !setXMLAttribute( att.getNodeName(), att.getNodeValue() ) )
					System.out.println( "Attribute not found: "
							+ att.getNodeName() + "=" + att.getNodeValue()
							+ "@" + getName() );
			}
		}
	}

	@Override
	public Button getCodeButton() {
		String s = "";
		for( int i = 0; i < getPointers(); i++ )
			s += "*";
		Button b = new Button( s + getName() );
		b.setOnAction( this );
		return b;
	}

}
